﻿
// ChildView.cpp: CChildView 类的实现
//

#include "stdafx.h"
#include "testWinaa.h"
#include "ChildView.h"
#include "CMyGraph.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define ST_POINT	1
#define ST_LINE		2
#define ST_TRI      3
#define ST_MID      4
#define ST_CLEAR	-1
#define ST_NONE		0

int MyMenuSt = 0;

CMyGraph graph;

// CChildView

CChildView::CChildView()
{
}

CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView, CWnd)
	ON_WM_PAINT()
	ON_COMMAND(ID_POINT, &CChildView::OnPoint)
	ON_COMMAND(ID_LINE, &CChildView::OnLine)
	ON_COMMAND(ID_TRI, &CChildView::OnTri)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_NONE, &CChildView::OnNone)
	ON_COMMAND(ID_SAVE, &CChildView::OnSave)
	ON_COMMAND(ID_LOAD, &CChildView::OnLoad)
	ON_COMMAND(ID_CLEAR, &CChildView::OnClear)
	ON_COMMAND(ID_MID, &CChildView::OnMid)
END_MESSAGE_MAP()



// CChildView 消息处理程序

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(nullptr, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), nullptr);

	return TRUE;
}

CDC *myDC = NULL;
void gdiDrawLine(T_Type x0, T_Type y0, T_Type x1, T_Type y1)
{
	myDC->MoveTo(x0, y0);
	myDC->LineTo(x1, y1);
}

void CChildView::OnPaint() 
{
	CPaintDC dc(this); // 用于绘制的设备上下文
	
	// TODO: 在此处添加消息处理程序代码
	myDC = &dc;

	graph.Draw();
	// 不要为绘制消息而调用 CWnd::OnPaint()
}



void CChildView::OnPoint()
{
	// TODO: 在此添加命令处理程序代码
	MyMenuSt = ST_POINT;
}

void CChildView::OnLine()
{
	// TODO: 在此添加命令处理程序代码
	MyMenuSt = ST_LINE;
}

void CChildView::OnTri()
{
	// TODO: 在此添加命令处理程序代码
	MyMenuSt = ST_TRI;
}

void CChildView::OnNone()
{
	// TODO: 在此添加命令处理程序代码
	MyMenuSt = ST_NONE;
}

void CChildView::OnMid()
{

	CClientDC dc(this);
	//CPen pen(PS_SOLID,penwidth,color);

	CBrush brush = RGB(255,0,0);
	CBrush* pbr = dc.SelectObject(&brush);

	dc.SelectObject(&brush);
	dc.Ellipse(300, 300, 400, 400);






	//HPEN hPen; //画笔;                  
	//hPen = CreatePen(PS_DASH, 2, 0x00ff00);//生成绿色画笔;         
	//SelectObject(myDC->m_hDC, hPen);//把画笔引入设备场境 ;   
	//Arc(myDC->m_hDC, 100, 100, 300, 300, 350, 500, 350, 500);//画圆 ;
	//SelectObject(myDC->m_hDC, hPen);//复原 ;
}

void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	static vector<int> buf;
	int x, y;

	switch (MyMenuSt)
	{
	case ST_POINT:
		CMyPoint *pt;
		pt = new CMyPoint();
		pt->SetPoint(point.x, point.y);
		graph.InsertObj(pt);
		Invalidate();
		break;
	case ST_LINE:
		if (buf.size() > 0)
		{
			x = buf[0];
			y = buf[1];
			CMyLine *pt;
			pt = new CMyLine();
			pt->SetPoint(0, x, y);
			pt->SetPoint(1, point.x, point.y);
			graph.InsertObj(pt);
			buf.clear();
			Invalidate();
		}
		else
		{
			buf.push_back(point.x);
			buf.push_back(point.y);
		}
		break;
	case ST_TRI:
		if (buf.size() >= 4)
		{
			CMyTri *pt;
			pt = new CMyTri();
			x = buf[0];
			y = buf[1];
			pt->SetPoint(0, x, y);
			x = buf[2];
			y = buf[3];
			pt->SetPoint(1, x, y);
			pt->SetPoint(2, point.x, point.y);
			graph.InsertObj(pt);
			buf.clear();
			Invalidate();
		}
		else
		{
			buf.push_back(point.x);
			buf.push_back(point.y);
		}
		break;
	case ST_NONE:
		buf.clear();
		break;
	}
	CWnd::OnLButtonDown(nFlags, point);
}


void CChildView::OnSave()
{
	CString filter = L"文本文档(*.txt)|*.txt||";
	CFileDialog dlg(FALSE, NULL, NULL, 0, filter);
	if (dlg.DoModal() != IDOK)
		return;

	fstream out(dlg.GetPathName(), ios::out);
	graph.Save(out);
	out.close();
}


void CChildView::OnLoad()
{

	CString filter = L"文本文档(*.txt)|*.txt||";
	CFileDialog dlg(TRUE, NULL, NULL, 0, filter);
	if (dlg.DoModal() != IDOK)
		return;

	fstream in(dlg.GetPathName(), ios::in);
	graph.Load(in);
	in.close();
	Invalidate();
}


void CChildView::OnClear()
{
	graph.Clear();
	Invalidate();
}



